package utils.data
{
	import com.adobe.utils.DateUtil;
	
	import components.valueobjects.AddressVO;
	import components.valueobjects.DateVO;
	import components.valueobjects.EventVO;
	import components.valueobjects.GeoLocationVO;
	import components.valueobjects.HotelVO;
	import components.valueobjects.LocationVO;
	import components.valueobjects.OpnvVO;
	import components.valueobjects.PersonVO;
	import components.valueobjects.SightVO;
	import components.valueobjects.TourVO;
	
	import flash.xml.XMLDocument;
	
	import mx.collections.ArrayCollection;
	import mx.rpc.xml.SimpleXMLDecoder;
	
	import utils.filemanager.MyFileWriter;

	/**
	 * Diese Klasse MyDataProvider bildet das Bindeglied zwischen den lokal verfügbaren Dateien
	 * und den Objekten, die aus den XML-Dateien erzeugt werden. 
	 * 
	 * Zu jeder Oberfläche gibt es eine entsprechende Methode, die die geparsten Daten aus den
	 * XML-Strukturen an die oberfläche übergibt
	 */
	
	
	public class MyDataProvider
	{
		public function MyDataProvider()
		{
		}
		
		public static function getAllEventData():ArrayCollection{
			return getArrayCollection("files/events.xml",EventVO.TYPE_EVENT);
		}
		public static function getAllTourData():ArrayCollection{
			return getArrayCollection("files/tours.xml",TourVO.TYPE_TOUR);
		}
		
		public static function getAllLocationData():ArrayCollection{
			var allCollection:ArrayCollection = new ArrayCollection();
			allCollection.addAll(getArrayCollection("files/hotels.xml",LocationVO.TYPE_HOTEL));
			allCollection.addAll(getArrayCollection("files/sights.xml",LocationVO.TYPE_SIGHT));
			allCollection.addAll(getArrayCollection("files/opnv.xml",LocationVO.TYPE_OPNV));
			
			return allCollection;
		}
		
		
		public static function getArrayCollection(xmlFilePath:String,type:String):ArrayCollection{
			var xml:XML = new XML(MyFileWriter.readIntern(xmlFilePath));
			var myDecoder:SimpleXMLDecoder = new SimpleXMLDecoder();
			var xmlList:XMLList = xml.children();
			var data:Object=myDecoder.decodeXML(new XMLDocument(xmlList.toXMLString()));
			
			switch(type)
			{
				case LocationVO.TYPE_SIGHT:
				{
					return getSightCollection(new ArrayCollection(data.sight));
					break;
				}
				case LocationVO.TYPE_HOTEL:
				{
					return getHotelCollection(new ArrayCollection(data.hotel));
					break;
				}
				case LocationVO.TYPE_OPNV:
				{
					return getOpnvCollection(new ArrayCollection(data.opnv));
					break;
				}
				case EventVO.TYPE_EVENT:
				{
					return getEventCollection(new ArrayCollection(data.event));
					break;
				}
				case TourVO.TYPE_TOUR:
				{
					return getTourCollection(new ArrayCollection(data.tour));
				}
				
			}
			
			return null;
			
		}
		
		private static function getSightCollection(rawData:ArrayCollection):ArrayCollection{
			var sightCollection:ArrayCollection = new ArrayCollection();
			for each (var obj:Object in rawData) 
			{
				sightCollection.addItem(new SightVO(obj.title,obj.img,obj.description,new GeoLocationVO(obj.geolocation.lat,obj.geolocation.lon),obj.mapurl,obj.hours,obj.price,obj.url,obj.phone,new AddressVO(obj.address.street,obj.address.postal,obj.address.city)));
			}
			return sightCollection;
		}
		
		private static function getHotelCollection(rawData:ArrayCollection):ArrayCollection{
			var hotelCollection:ArrayCollection = new ArrayCollection();
			for each (var obj:Object in rawData) 
			{
				hotelCollection.addItem(new HotelVO(obj.title,obj.img,obj.description,new GeoLocationVO(obj.geolocation.lat,obj.geolocation.lon),obj.mapurl,obj.url,obj.email,obj.phone,new AddressVO(obj.address.street,obj.address.postal,obj.address.city)));
			}
			return hotelCollection;
		}
		
		private static function getOpnvCollection(rawData:ArrayCollection):ArrayCollection{
			var opnvCollection:ArrayCollection = new ArrayCollection();
			for each (var obj:Object in rawData) 
			{
				opnvCollection.addItem(new OpnvVO(obj.title,obj.description,new GeoLocationVO(obj.geolocation.lat,obj.geolocation.lon),obj.mapurl,obj.opnvtype));
			}
			return opnvCollection;
		}
		
		private static function getTourCollection(rawData:ArrayCollection):ArrayCollection{
			var tourCollection:ArrayCollection = new ArrayCollection();
			for each (var obj:Object in rawData) 
			{
				var guide:PersonVO = new PersonVO(obj.guide.firstname,obj.guide.lastname,obj.guide.company,obj.guide.phone,obj.guide.mobile,obj.guide.email,obj.guide.img);
				tourCollection.addItem(new TourVO(obj.title,obj.description,obj.img,obj.meetingpoint,obj.periods,obj.duration,obj.price,obj.stations,obj.url,obj.phone,obj.mapimg,guide));
			}
			return tourCollection;
		}
		
		private static function getEventCollection(rawData:ArrayCollection):ArrayCollection{
			var eventCollection:ArrayCollection = new ArrayCollection();
			for each (var obj:Object in rawData) 
			{
				var startDate:Date = DateUtil.parseW3CDTF(obj.date.start);
				var endDate:Date = DateUtil.parseW3CDTF(obj.date.end);
				var dateVO:DateVO = new DateVO(startDate,endDate,Boolean (obj.date.allday));

				eventCollection.addItem(new EventVO(obj.title,obj.img,obj.description,obj.location,obj.url,obj.phone,obj.price,obj.hours,new AddressVO(obj.address.street,obj.address.postal,obj.address.city), dateVO,obj.date.start, obj.date.end, obj.mapurl));
			}
			return eventCollection;
		}
	}
}